﻿@model EC18_ESHOP.Models.ProductModel

@{
    ViewBag.Title = "AddNewProduct";
    Layout = "~/Areas/Admin/Views/Shared/_Admin.cshtml";
}
<script src="~/Content/ckeditor/ckeditor.js"></script>
<div class="page-header position-relative">
    <h1>Quản lý sản phẩm
		<small>
            <i class="icon-double-angle-right"></i>
            Đăng sản phẩm mới
        </small>
    </h1>
    @{
        int status = ViewBag.Status;
        switch (status)
        {
            case 1: <label style="color: green; font-size: large">Đăng sản phẩm thành công</label>
                break;
            case 2: <label style="color: red; font-size: large">Sản phẩm này đã tồn tại</label>
                break;
            case 3: <label style="color: red; font-size: large">Đăng sản phẩm thất bại</label>
                break;
        }
    }
</div>
<!--/.page-header-->

<div class="row-fluid span12">
    @using (Html.BeginForm("addNewProduct", null, FormMethod.Post, new { @role = "form", enctype = "multipart/form-data" }))
    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)

        <div class="form-horizontal">
            <div class="control-group">
                <label class="control-label" for="ulImages">Mã sản phẩm: </label>
                <div class="controls">
                    @Html.EditorFor(model => model.ProductId)
                    @Html.ValidationMessageFor(model => model.ProductId)
                </div>
            </div>

            <div class="control-group">
                <label class="control-label" for="ulImages">Tên sản phẩm</label>
                <div class="controls">
                    @Html.EditorFor(model => model.ProductName)
                    @Html.ValidationMessageFor(model => model.ProductName)
                </div>
            </div>

            <div class="control-group">
                <label class="control-label" for="ulImages">Giá sản phẩm</label>
                <div class="controls">
                    @Html.EditorFor(model => model.ProductPrice, new { @type = "number" })
                    @Html.ValidationMessageFor(model => model.ProductPrice)
                </div>
            </div>

            <div class="control-group">
                <label class="control-label" for="ulImages">Danh mục</label>
                <div class="controls">
                    <div class="widget-box span3">
                        <div id="category">
                            @Html.DropDownList("CategoryId", null, new { @class = "form-control" })
                            @Html.ValidationMessageFor(model => model.CategoryId)
                        </div>
                    </div>
                    <div class="widget-box span6">
                        <div id="accordion1" class="accordion">
                            <div class="accordion-group">
                                <div class="accordion-heading">
                                    <a href="#collapseTwo" data-parent="#accordion1" data-toggle="collapse" class="accordion-toggle collapsed">Thêm danh mục mới</a>
                                </div>

                                <div class="accordion-body collapse" id="collapseTwo" style="height: 0">
                                    <div class="accordion-inner">
                                        <table>
                                            <tr>
                                                <td>
                                                    <label>Tên danh mục:</label></td>
                                                <td>
                                                    <input id="txtCategoryName" type="text" /></td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    <label>Danh mục cha: </label>
                                                </td>
                                                <td>
                                                    <select id="categoryParent">
                                                        @{List<EC18_ESHOP.Models.CATEGORY> arrCategory = ViewBag.arrCategory;
                                                          if (arrCategory.Count > 0)
                                                          {
                                                            <option value="0">null</option>
                                                              foreach (EC18_ESHOP.Models.CATEGORY item in arrCategory)
                                                              {<option value="@item.Category_Id">@item.CategoryName</option>}
                                                          }
                                                        }
                                                    </select>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td colspan="2" style="text-align: right">
                                                    <button class="btn btn-mini btn-danger" type="button" onclick="addNewCategory()" id="btnAddNewCategory" name="btnAddNewCategory">Thêm mới</button>
                                                </td>
                                            </tr>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="control-group">
                <label class="control-label" for="ulImages">Nhà sản xuất</label>
                <div class="controls">
                    <div class="widget-box span3">
                        <div id="manufacturer">
                            @Html.DropDownList("ManufacturerId", null, new { @class = "form-control" })
                            @Html.ValidationMessageFor(model => model.ManufacturerId)
                        </div>
                    </div>
                    <div class="widget-box span6">
                        <div id="accordion2" class="accordion">
                            <div class="accordion-group">
                                <div class="accordion-heading">
                                    <a href="#collapseOne" data-parent="#accordion2" data-toggle="collapse" class="accordion-toggle collapsed">Thêm nhà sản xuất mới</a>
                                </div>

                                <div class="accordion-body collapse" id="collapseOne" style="height: 0;">
                                    <div class="accordion-inner">
                                        <table>
                                            <tr>
                                                <td>Tên nhà sản xuất: </td>
                                                <td>
                                                    <input id="txtManufacturerName" type="text" />
                                                </td>
                                            </tr>
                                            <tr>
                                                <td colspan="2" style="text-align: right">
                                                    <button class="btn btn-mini btn-danger" type="button" onclick="addNewManufacturer()">Thêm mới</button>
                                                </td>
                                            </tr>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
        </div>
        <h5 class="header green clearfix">Mô tả</h5>
        @Html.TextAreaFor(model => model.Description, new { @class = "ckeditor", rows = "10", cols = "80" })
        @Html.ValidationMessageFor(model => model.Description)
        <div class="imgUpdate">
            <h5 class="header green clearfix">Hình ảnh sản phẩm</h5>
            <div class="controls">
                <input type="file" name="ProductPicture1" value="" /><br />
                <input type="file" name="ProductPicture2" value="" /><br />
                <input type="file" name="ProductPicture3" value="" /><br />
            </div>

            <div class="space-4"></div>
            <div class="form-horizontal">
                <div class="form-actions">
                    <button class="btn btn-info" type="submit">
                        <i class="icon-ok bigger-110"></i>
                        Đăng sản phẩm
                    </button>
                    <button class="btn" type="reset">
                        <i class="icon-undo bigger-110"></i>
                        Hủy
                    </button>
                </div>
            </div>
        </div>   
    }
</div>
<!----------->

<script type="text/javascript">
    function getSelectedText(elementId) {
        var elt = document.getElementById(elementId);
        if (elt.selectedIndex == -1)
            return null;
        return elt.options[elt.selectedIndex].value;
    }

    function addNewCategory() {
        var val = {
            categoryParent: getSelectedText('categoryParent'),
            categoryName: document.getElementById('txtCategoryName').value
        }
        $.post('/Admin/AdminHome/addCategory/', val, function (data) {
            document.getElementById("category").innerHTML = data.data;
        });
    }

    function addNewManufacturer() {
        var val = {
            manufacturerName: document.getElementById('txtManufacturerName').value
        }
        $.post('/Admin/AdminHome/addManufacturer/', val, function (data) {
            document.getElementById("manufacturer").innerHTML = data.data;
        });
    }
</script>
